<?php
namespace jy_baojdai;
defined('BY_JYA') or exit('error');
/*
	代理插件工具类
*/
class model extends \plugin{
	
	public function create_condition($conditions){
		if(!$conditions){
			return '';
		}
		if(!is_array($conditions)){
			$conditions=json_decode($conditions,true);
		}
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->current_level_up_types();
		$str='';
		foreach($current_level_up_types as $key){
			$str.=$level_up_types[$key].'达'.$conditions[$key].';';
		}
		return $str;
	}
	public function current_level_up_types($is1=0){
		$level_up_types=$is1?$this->setting['level_up_types1']:$this->setting['level_up_types'];
	    $level_up_types=str_array($level_up_types);
	    foreach ($level_up_types as $k=>$lv){
	        if(!$this->level_up_types[$lv]){
	            unset($level_up_types[$k]);
            }
        }
		return $level_up_types;
	}
	public function otherfee_types(){
		$jy_renwushop = check_permission('jy_renwushop');
		$res=array(
			'0'=>'无',
		);
		if($jy_renwushop){
			$res['jy_renwushop']='任务商城分佣';
		}
		return $res;
	}
	public function get_area_code($area){
		if(!$area['province']){
			return '';
		}
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function upgoods($post){
		if(!$post['id'] && !$post['ignore'] && empty($post['commissions'])){
			return false;
		}
		$data=array(
			'goods_id'=>intval($post['goods_id']),
			'ignore'=>intval($post['ignore']),
			'level_id'=>intval($post['level_id']),
			'commissions'=>$post['commissions']?ijson_encode($post['commissions']):'',
		);
		$id=intval($post['id']);
		if($id){
			$this->update('goods',$data,array('id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$this->insert('goods',$data);
		}
		return true;
	}
	public function getgoodshtml($goods_id){
		$commission_goods=pdo_get('jy_baojdai_goods',array('goods_id'=>$goods_id,'uniacid'=>$_SESSION['uniacid']));
		$return['item']['jy_baojdai_id']=$commission_goods['id'];
		$return['item']['jy_baojdai']=$commission_goods['commissions']=$commission_goods['commissions']?json_decode($commission_goods['commissions'],true):array();
		$return['item']['jy_baojdai_ignore']=$commission_goods['ignore']?:0;
		$htmls=array();
		$htmls[]=array('title'=>'客户代理设置','type'=>'title');
		$htmls[]=array('name'=>'jy_baojdai_ignore','title'=>'是否关闭代理','type'=>'switch','help'=>'关闭后，该商品将不参与代理');
		$htmls[]=array('title'=>'[温馨提示]：默认等级适合所有等级（当不匹配等级的时候，都按默认等级的比例算！）','type'=>'tip');
		$commissions=array();
		$commissions[]=array('name'=>'level_id','title'=>'适用等级','type'=>'select','help'=>'','options'=>'jy_baojdailevels');
		$commissionset=plugin('jy_baojdai')->plugin_setting('setting');
		for($i=1;$i<=$commissionset['level'];$i++){
			$commissions[]=array('type'=>'string','name'=>'commission'.$i,'title'=>$i.'级佣金','help'=>'填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金');
		}
		if($commissionset['area_open']==1){
			$commissions[]=array('type'=>'switch','name'=>'area_open','title'=>'是否开启社区代理');
			$commissions[]=array('name'=>'area_rate','title'=>'社区代理佣金','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金');
			$commissions[]=array('name'=>'area_xunirate','title'=>'社区代理虚拟币','type'=>'string','help'=>'填写数字表示固定'.'，填写%，表示按百分比计算虚拟币');
		}
		$commissions[]=array('type'=>'string','name'=>'xuni_rate','title'=>'直推获得虚拟币','help'=>'填写数字表示实际额度，填写百分数表示比例。例如填写1表示发放1个虚拟币，填写1%表示发放商品价格1%的虚拟币');	
		$htmls[]=array('type'=>'array','show'=>array('jy_baojdai_ignore'=>0),'title'=>'佣金设置','name'=>'jy_baojdai','add_tip'=>'添加一个设置',
					'list'=>$commissions,
					'details'=>$commissions
		);
		$return['htmls']=$htmls;
		$page_setting=$this->plugin_setting('page_setting');
		$setting=$this->plugin_setting('setting');
		$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
		$return['levels']['0']=array('id'=>0,'title'=>$page_setting['demolevel']);
		return $return;
	}
	public function buy_creditfee($order,$member=array()){
		if(!$member['level_id']){
			$default_level=$this->plugin_setting('default_level');
			$feetype=$default_level['feetype'];
			$buy_creditfee=$default_level['buy_creditfee'];
			$buy_creditfrom=$default_level['buy_creditfrom'];
		}else{
			$item=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['commissions']=json_decode($item['commissions'],true);
			$feetype=$item['feetype'];
			$buy_creditfee=$item['commissions']['buy_creditfee'];
			$buy_creditfrom=$item['buy_creditfrom'];
		}
		$fee=$order['price']*$buy_creditfee/100;
		if(!$buy_creditfee || $fee<=0){
			return false;
		}
		$ispay=1;
		if($member['agent_id'] && $buy_creditfrom){
			$basic=model('core/member')->detail($member['agent_id'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
			if($basic['credit'.$feetype]>=$fee){
				model('core/member')->member_credit($member['agent_id'],'credit'.$feetype,$fee,false,'jy_baojdai','下级下单奖励给下级');	
			}else{
				$ispay=0;
			}
		}
		if($ispay){
			model('core/member')->member_credit($member['mid'],'credit'.$feetype,$fee,true,'jy_baojdai','下单奖励');
		}
		$feelog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'agent_id'=>$member['agent_id'],
				'fee'=>$fee,
				'feetype'=>$feetype,
				'createtime'=>TIMESTAMP,
				'ispay'=>$ispay,
		);
		$this->insert('feelog',$feelog);
		return true;
	}
}